package cn.edu.swufe.stu2025.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.renderscript.ScriptGroup;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class VisitInternet extends AppCompatActivity implements Runnable{
    String Tag="aaw";
    Handler handler;
    double dollarRate=0.00;
    double euroRate=0.00;
    double wonRate=0.00;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_visit_internet);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Log.i(Tag,"启动线程");
        Thread t = new Thread(this);
        t.start();

        handler=new Handler(Looper.getMainLooper()){
                public void handleMessager(Message msg) {
                    Log.i(Tag,"handleMessage：接收消息");
                    if(msg.what==7){
                        String str = (String)msg.obj;
                        Log.i(Tag,"handleMessage："+str);

                        Bundle bdl2=(Bundle) msg.obj;
                        dollarRate=bdl2.getFloat("web_dollar");
                        euroRate=bdl2.getFloat("web_euro");

                    }
                    super.handleMessage(msg);
                }
        };
    }


    public String inputStream2String (InputStream inputStream) throws IOException {
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(inputStream, "gb2312");
        while (true) {
            Log.i(Tag,"inputStream2String");
            int rsz = in.read(buffer, 0, buffer.length);
            if (rsz < 0) break;
            out.append(buffer, 0, rsz);
        }
        return out.toString();
    }
    @Override
    public void run() {
        Log.i(Tag, "running…………");
            URL url = null;
            try {
                url = new URL("https://www.huilvwang.com/");
                HttpURLConnection http = (HttpURLConnection) url.openConnection();
                InputStream in = http.getInputStream();
                String html = inputStream2String(in);
                Log.i(Tag, "run url=" + url);
                Log.i(Tag,html);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}